package com.cljz.makepassenger.present.activity;

import android.graphics.BitmapFactory;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.PublishReqBean;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.GetTypeListBean;
import com.cljz.makepassenger.model.net.bean.InvitationBean;
import com.cljz.makepassenger.model.net.bean.InviteDetailBean;
import com.cljz.makepassenger.model.net.bean.PublishBean;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.model.net.bean.UploadImgBean;
import com.cljz.makepassenger.ui.activity.InvitationLetterActivity;
import com.cljz.makepassenger.utils.BitmapUtils;
import com.cljz.makepassenger.utils.L;
import com.cljz.makepassenger.utils.date.GetSystemTimeUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/4/10.
 */

public class InvitationLetterP extends BaseP<InvitationLetterActivity> {

    private List<List<InvitationBean.DataBean>> invitationList;


    public void getInviteDetail(String projectId, int type) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        if (type == InvitationLetterActivity.INVITE) {
            paramsMap.put("invite_id", projectId);
        } else if (type == InvitationLetterActivity.ACTIVITY) {
            paramsMap.put("activity_id", projectId);
        }
        L.e(gson.toJson(paramsMap));
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getInviteDetailData(requestBody)
                .compose(XApi.<InviteDetailBean>getApiTransformer())         //异常处理
                .compose(XApi.<InviteDetailBean>getScheduler())              //线程切换
                .compose(getV().<InviteDetailBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<InviteDetailBean>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().inviteDetailFail(error.getMessage());
                    }

                    @Override
                    public void onNext(InviteDetailBean inviteDetailBean) {
                        if ("0".equals(inviteDetailBean.getError_code())) {
                            getV().inviteDetailSucceed(inviteDetailBean.getData());
                        } else if ("1000".equals(inviteDetailBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().inviteDetailFail(inviteDetailBean.getMsg());
                        }
                    }
                });
    }

    public void getInviteCancel(String projectId) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("invite_id", projectId);
        L.e("project",paramsMap.toString());
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getInviteCancelData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())         //异常处理
                .compose(XApi.<ResponseBean>getScheduler())              //线程切换
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().inviteCancelSucceed(responseBean.getMsg());
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().inviteCancelFail(responseBean.getMsg());
                        }
                    }
                });
    }

    public void upLoadImg(List<String> picList) {
        try {
            JSONObject jsonObject = new JSONObject();
            JSONArray array = new JSONArray();
            for (int i = 0; i < picList.size(); i++) {
                L.e("upLoadImg:" + picList.get(i).substring(picList.get(i).lastIndexOf(".") + 1));
                JSONObject inner = new JSONObject();
                String imgByte = BitmapUtils.convertPicToString(BitmapFactory.decodeFile(picList.get(i)));
                inner.put("content", imgByte);
                inner.put("ext", picList.get(i).substring(picList.get(i).lastIndexOf(".") + 1));
                array.put(inner);
            }
            jsonObject.put("token", getToken());
            jsonObject.put("type", 2);
            jsonObject.put("file", array);
            String body = Base64Utils.enCode(jsonObject.toString());
            RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
            Api.getResponseInfoAPI().getUpLoadImgData(requestBody)
                    .compose(XApi.<UploadImgBean>getApiTransformer())         //异常处理
                    .compose(XApi.<UploadImgBean>getScheduler())              //线程切换
                    .compose(getV().<UploadImgBean>bindToLifecycle())
                    .subscribe(new ApiSubcriber<UploadImgBean>() {
                        @Override
                        protected void onFail(NetError error) {
                            L.e(error.toString());
                        }

                        @Override
                        public void onNext(UploadImgBean uploadImgBean) {
                            if ("0".equals(uploadImgBean.getError_code())) {
                                getV().upLoadImgSucceed(uploadImgBean.getData());
                            } else if ("1000".equals(uploadImgBean.getError_code())) {
                                getV().loginOvertime(1);
                            } else {
                                getV().upLoadImgFail(uploadImgBean.getMsg());
                            }
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void publish(final PublishReqBean publishReqBean) {

        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("invite_time", GetSystemTimeUtil.switchTimestamp(publishReqBean.getInvite_time()));
        paramsMap.put("landmark", publishReqBean.getLandmark());
        paramsMap.put("address", publishReqBean.getAddress());
        paramsMap.put("title", publishReqBean.getTitle());
        paramsMap.put("region_id3", publishReqBean.getRegion_id3());
        paramsMap.put("longitude", publishReqBean.getLongitude());
        paramsMap.put("latitude", publishReqBean.getLatitude());
        paramsMap.put("invitation_id", publishReqBean.getInvitation_id());
        paramsMap.put("is_activity", publishReqBean.getIs_activity());
        paramsMap.put("title2", publishReqBean.getTitle2());
        paramsMap.put("type_id", publishReqBean.getType_id());
        paramsMap.put("number_all", publishReqBean.getNumber_all());
        paramsMap.put("content", publishReqBean.getContent());
        paramsMap.put("time_start", GetSystemTimeUtil.switchTimestamp(publishReqBean.getTime_start()));
        paramsMap.put("time_end", GetSystemTimeUtil.switchTimestamp(publishReqBean.getTime_end()));
        paramsMap.put("img_path", publishReqBean.getImg_path());
        paramsMap.put("img_other", publishReqBean.getImg_other());
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().publishData(requestBody)
                .compose(XApi.<PublishBean>getApiTransformer())
                .compose(XApi.<PublishBean>getScheduler())
                .compose(getV().<PublishBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<PublishBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(PublishBean publishBean) {
                        if ("0".equals(publishBean.getError_code())) {
                            getV().publishSucceed(publishBean.getData());
                        } else if ("1000".equals(publishBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().publishFail(publishBean.getMsg());
                        }
                    }
                });
    }

    public void collection(String activityId) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("activity_id", activityId);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().collectionData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().collectionSucceed(responseBean.getMsg());
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().collectionFail(responseBean.getMsg());
                        }
                    }
                });
    }

    public void collectionCancel(String activityId) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("activity_id", activityId);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().collectionCancelData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().collectionCancelSucceed(responseBean.getMsg());
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().collectionCancelFail(responseBean.getMsg());
                        }
                    }
                });
    }

    public void inviteAccept(String projectId) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("activity_id", projectId);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().inviteAccept(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().inviteAcceptSucceed(responseBean.getMsg());
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().inviteAcceptFail(responseBean.getMsg());
                        }
                    }
                });
    }


    public void getTypeList() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("type", "invite");
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getTypeListData(requestBody)
                .compose(XApi.<GetTypeListBean>getApiTransformer())
                .compose(XApi.<GetTypeListBean>getScheduler())
                .compose(getV().<GetTypeListBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<GetTypeListBean>() {
                    @Override
                    protected void onFail(NetError error) {

                    }

                    @Override
                    public void onNext(GetTypeListBean getTypeListBean) {
                        if ("0".equals(getTypeListBean.getError_code())) {
                            getV().getTypeListSucceed(getTypeListBean.getData());
                        } else if ("1000".equals(getTypeListBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getTypeListFail(getTypeListBean.getMsg());
                        }
                    }
                });
    }

    public void getInvitation(final List<GetTypeListBean.DataBean> dataBeanList) {
        invitationList = new ArrayList<>();
        for (int i = 0; i < dataBeanList.size(); i++) {
            HashMap<String, String> paramsMap = new HashMap<>();
            paramsMap.put("token", getToken());
            paramsMap.put("id", dataBeanList.get(i).getId());
            String body = Base64Utils.enCode(gson.toJson(paramsMap));
            RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
            Api.getResponseInfoAPI().invitationData(requestBody)
                    .compose(XApi.<InvitationBean>getApiTransformer())
                    .compose(XApi.<InvitationBean>getScheduler())
                    .compose(getV().<InvitationBean>bindToLifecycle())
                    .subscribe(new ApiSubcriber<InvitationBean>() {
                        @Override
                        protected void onFail(NetError error) {
                        }
                        @Override
                        public void onNext(InvitationBean getTypeListBean) {
                            if ("0".equals(getTypeListBean.getError_code())) {
                                invitationList.add(getTypeListBean.getData());
                                if (invitationList.size() == dataBeanList.size()) {
                                    getV().getInvitationFinish(invitationList);
                                }
                            } else if ("1000".equals(getTypeListBean.getError_code())) {
                                getV().loginOvertime(1);
                            } else {
                                getV().getTypeListFail(getTypeListBean.getMsg());
                            }
                        }
                    });
        }
    }
}
